package com.vancouver.knows.ui.activity.login;

import com.pangu.AMApplication;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.ListItemBrowser;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;

@SuppressWarnings("rawtypes")
public class ChooseCountryView extends ListItemBrowser<AMApplication> { 
	public View head;
	private Context mContext;
	private Handler mHandler;
	private String[] mCodeArea = {"001","86","00852","00853","00886"};
	private String[] mAttrTitles;
	private String  mCurrentActivity;
	public ChooseCountryView(Context context, Handler handler, String currentActivity) {
		super(context);
		mContext = context;
		mHandler = handler;
		mCurrentActivity = currentActivity;
		mAttrTitles = context.getResources().getStringArray(R.array.country_titles);
		loadData();
	}

	private void loadData() {
		BusinessModule module = ((MainApplication) imContext).getBusinessModule();
		ATaskMark aTaskMark = module.getTaskMarkPool().getCountryChooseTaskMark();
		if (aTaskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			aTaskMark.reinitTaskMark();
		}
		aTaskMark.setTaskStatus(ATaskMark.HANDLE_OVER);
		ChooseCountryView.this.initLoadableView(aTaskMark);
	}
	
	private void sendChooseCountryBroadCast(String code, String area) {
		Intent intent = new Intent();
		intent.putExtra("AREA_CODE", code);
		intent.putExtra("COUNTRY", area);
		if (mCurrentActivity.equals(PhoConstants.CURRENT_OPEN_FROM_LOGIN)){
			intent.setAction(PhoConstants.CHOOSE_COUNTRY_INFO_LOGIN);
		}else if (mCurrentActivity.equals(PhoConstants.CURRENT_OPEN_FROM_REGISTER_PHONE)){
			intent.setAction(PhoConstants.CHOOSE_COUNTRY_INFO_REGISTER_PHONE);
		}else if (mCurrentActivity.equals(PhoConstants.CURRENT_OPEN_FROM_FORGET_PSD)){
			intent.setAction(PhoConstants.CHOOSE_COUNTRY_INFO_FORGET_PSD);
		}
		mContext.sendBroadcast(intent);
	}
	
	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDividerHeight(0);
		listView.setCacheColorHint(0);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (mCodeArea != null && mCodeArea.length > 0){
			sendChooseCountryBroadCast(mCodeArea[position], mAttrTitles[position]);
			Message msg = new Message();

			mHandler.sendMessage(msg);
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new ChooseCountryListAdapter();
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
		
	}
	
	public class ChooseCountryListAdapter extends BaseAdapter{
		private LayoutInflater lf;
		
		public ChooseCountryListAdapter(){	
			this.lf = LayoutInflater.from(getContext());
		}
		@Override
		public int getCount() {
			return mCodeArea.length;
		}

		@Override
		public String getItem(int position) {
			return mCodeArea[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewValueHand hand = null;
			View view = convertView;
			if(convertView==null){
				view=lf.inflate(R.layout.login_choose_country_view,  parent, false);
				hand=new ViewValueHand(view);
				
				view.setTag(hand);
			}else{
				hand=(ViewValueHand) view.getTag();
			}
			hand.mAreaCode.setText(mCodeArea[position]);
			hand.mCountry.setText(mAttrTitles[position]);
			
			return view;
		}
		
		public class ViewValueHand {
			@Bind(R.id.list_layout)
			LinearLayout layout;
			@Bind(R.id.area_code)
			TextView mAreaCode;
			@Bind(R.id.country)
			TextView mCountry;
			
			public ViewValueHand(View view){
				ButterKnife.bind(this, view);
			}
		}
	}

}